"""
将数字转为中文字符串
@author hujing
@date 2020/6/26
"""

# 数字中文
DIGIT_STR_LIST = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九']
# 权重中文
WIGHT_STR_LIST = ['', '十', '百', '千']
# 分组后对应得中文
GROUP_STR_LIST = ['', '万', '亿', '兆']
# 零
ZERO_STR = '零'


def thousand_list_num_to_str(num_list: list) -> str:
    """
    将4位数字转成字符串
    :param num_list: 数字列表, 长度不超过4. 索引和数字得对应为: 个十百千
    :return:
    """
    # 保存每一位的内容
    result_list = []
    # 遍历数字的每一位, 将数组转列表并倒序遍历
    for wight, digit in enumerate(num_list):
        digit = int(digit)
        if digit is 0:
            # 个位的0无输出
            if wight is 0:
                continue
            # 连续0无输出
            elif int(num_list[wight-1]) is 0:
                continue
            # 直接拼零
            result_list.append(ZERO_STR)
            continue
        digit_str = DIGIT_STR_LIST[digit] if digit < len(DIGIT_STR_LIST) else ''
        wight_str = WIGHT_STR_LIST[wight] if wight < len(WIGHT_STR_LIST) else ''
        #
        if digit is 0:
            wight_str = ''
        # 结果拼接
        result_list.append(digit_str + wight_str)
    # 将结果倒序拼接
    result_list.reverse()
    return "".join(result_list)


def num_to_str(num : int) -> str:
    """
    将数组装成中文
    :param num:
    :return:
    """
    if num is 0:
        return ZERO_STR
    # 将数字切割为每四个一组, 分别进行处理
    num_list = list(str(num))
    # 这里为了处理长度不是4整数倍得情况, 提前反转.
    num_list.reverse()
    group_num_list = [num_list[i:i+4] for i in range(0, len(num_list), 4)]
    result_list = []
    # 遍历每一组, 并产生对应得中文输出
    for group, num_list in enumerate(group_num_list):
        # 若是0, 跳过
        if int(''.join(num_list)) is 0:
            continue
        this_num_str = thousand_list_num_to_str(num_list)
        group_str = GROUP_STR_LIST[group] if group < len(GROUP_STR_LIST) else ''
        result_list.append(this_num_str + group_str)
    result_list.reverse()
    return ''.join(result_list)


if __name__ == '__main__':
    print(num_to_str(123456789))